package com.bang.index.template;

import com.alibaba.fastjson.JSONObject;
import com.bang.EsUtils;
import org.apache.http.HttpEntity;
import org.apache.http.StatusLine;
import org.apache.http.entity.ContentType;
import org.apache.http.nio.entity.NStringEntity;
import org.elasticsearch.action.admin.indices.alias.Alias;
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest;
import org.elasticsearch.action.main.MainResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.*;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: TemplateApi
 * @Desc: TODO
 * @author: <Bang>
 * @Date: 2021/12/22 16:45
 * @Version: 1.0
 * @Modified By:
 */
public class TemplateApi {

    public static void main(String[] args) throws IOException {
        createIndexTemplate(EsUtils.getClient());
    }

    public static void putTemplate() throws IOException {
        RestHighLevelClient client = EsUtils.getClient();
        PutIndexTemplateRequest request = new PutIndexTemplateRequest("my-template");
        String source="{\"order\":0,\"template\":\"test-*\",\"settings\":{\"index\":{\"number_of_shards\":\"3\",\"number_of_replicas\":\"1\"}},\"mappings\":{},\"aliases\":{\"test\":{}}}";

        /**
         *
         * {
         *     "template": "test-*",
         *     "settings": {
         *         "index": {
         *             "number_of_shards": "3",
         *             "number_of_replicas": "1"
         *         }
         *     },
         *     "mappings": {},
         *     "aliases": {
         *         "test": {}
         *     }
         * }
         */
        request.source("{\n" +
                "    \"template\": \"test-*\",\n" +
                "    \"settings\": {\n" +
                "        \"index\": {\n" +
                "            \"number_of_shards\": \"3\",\n" +
                "            \"number_of_replicas\": \"1\"\n" +
                "        }\n" +
                "    },\n" +
                "    \"mappings\": {},\n" +
                "    \"aliases\": {\n" +
                "        \"test\": {}\n" +
                "    }\n" +
                "}",XContentType.JSON);
        AcknowledgedResponse putTemplateResponse = client.indices().putTemplate(request, RequestOptions.DEFAULT);
        System.out.println(putTemplateResponse.isAcknowledged());
    }

    private static int SUCCESS = 200;

    public static boolean createIndexTemplate(RestHighLevelClient client) {
        RestClient lowLevelClient = client.getLowLevelClient();
        try {
            HttpEntity entity = new NStringEntity("{\"template\":\"e_vaiops_jk_vpn-*\",\"settings\":{\"number_of_shards\":1},\"index_patterns\":[\"e_vaiops_jk_vpn-*\"],\"mappings\":{\"doc\":{\"properties\":{\"inDate\":{\"format\":\"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis\",\"type\":\"date\"}}}},\"aliases\":{\"e_vaiops_jk_vpn\":{}}}", ContentType.APPLICATION_JSON);
            Response response = lowLevelClient.performRequest("PUT","_template/template_vpn", Collections.emptyMap(),entity);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (SUCCESS == statusCode) {
                return true;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

}
